/***********************************************************************************
@file   lora
@author
@attention
模式（0-3）       M1  M0    模式介绍
0 传输模式        0   0     串口打开，无线打开，透明传输
1 WOR 发送模式    0   1     WOR 发送数据，WOR 接收数据
2 WOR 接收模式    1   0     WOR 发送关闭，WOR 接收数据
3 休眠模式        1   1     可配置参数
***********************************************************************************/
#include "lora.h"
/***********************************************************************************
 * @para LORA模组参数
***********************************************************************************/
uint8_t lora_tb[30];                               //串口发送缓存
uint8_t lora_receive_flag;                         //串口接收数据标志
uint8_t lora_receive_ok;                           //串口接受完成标志
uint8_t lora_rlen;                                 //串口接收到的数据长度
uint8_t lora_receive_cnt;						   //接收到数据开始计时
uint8_t lora_rb[50];                               //串口接收缓存
uint8_t lora_flag;         

static  uint16_t lora_config_cnt;
/***********************************************************************************
 * @brief  lora通讯接口
 * @retval 
***********************************************************************************/
void lora_transmit(uint8_t* data,uint16_t len)
{
//    uart_send(data,len);
    lpuart_send(data,len);
}
/***********************************************************************************
 * @brief  lora初始化
 * @retval 
***********************************************************************************/
uint8_t lora_init(void)
{
    uint8_t i=0;;
    lora_m0(1);
    lora_m1(1);
    delay_ms(100);
    
    lora_tb[0]=0xc0;                    //数据头
    lora_tb[1]=0x00;                    //起始寄存器
    lora_tb[2]=0x05;                    //寄存器数量
    lora_tb[3]=0x00;                    //00_ADDR_H
    lora_tb[4]=0x00;                    //01_ADDR_L
    lora_tb[5]=0x62;                    //02_9600_8N1_2.4k
    lora_tb[6]=0x00;                    //03_暂保持0
    lora_tb[7]=23+system_data.lora_channel*5;            //04_CH_433

    while(i<3)
    {
        system_wdg_refresh();
        lora_transmit(lora_tb,8);
        delay_ms(200);
        
        if(lora_rb[0]==0xc1)
        {
            memset(lora_rb, 0, sizeof(lora_rb));
            lora_receive_ok=0;
            return 1;
        }
        i++; 
//        printf("lora_init\r\n");
    }
    return 0;
}
/***********************************************************************************
 * @brief  lora发送
 * @retval 
***********************************************************************************/
void lora_send_message(uint8_t off)
{
     uint16_t crc;
    static uint16_t value_old=11111;
    static uint8_t states_old;
    uint8_t dynamic_flag;
    
    dynamic_flag = (mydata > value_old) ? 
               ((mydata - value_old) > system_data.data_stability): 
               ((value_old - mydata) > system_data.data_stability);
    
    if((states_old!=status)||(off)||(dynamic_flag)||(time_send_lora==0)||\
      ((goto_lowpower==0)&&(power_low1)&&(start_averrage)&&(status==0))||
      (notic_lowpower))
    {
        if((system_data.myaddress)&&(system_data.host_address[0]))
        {
            lora_m0(0);
            lora_m1(0);   
            states_old=status;
            value_old=mydata;
            
            #if (DEBUG == 0)
            time_send_lora=TIME_SEND_LORA;
            #else
            time_send_lora=120;//TIME_SEND_LORA;
            #endif
                       
            delay_ms(20);
            _Sensor_data_struct* sensor_data=(_Sensor_data_struct*)&lora_tb[7]; 
            lora_tb[0] = 0xb1;
            lora_tb[1] = system_data.host_address[0]>>8;
            lora_tb[2] = system_data.host_address[0];
            lora_tb[3] = 0x03;
            lora_tb[4] = 12;
            lora_tb[5] = 1;
            lora_tb[6] = system_data.myaddress-1;
            sensor_data->gas    = system_data.gastype;
            sensor_data->value  = mydata;
            switch(system_data.precision)
            {
                case 1:
                {
                    sensor_data->propor = 0;
                    break;
                }
                case 10:
                {
                    sensor_data->propor = 1;
                    break;
                }
                case 100:
                {
                    sensor_data->propor = 2;
                    break;
                }
                default:
                {
                    break;
                }
            }
            sensor_data->unit = system_data.unit-1;
            if(off)
            {
                sensor_data->state  = 10;
            }
            else
            if(((power_low1)&&(start_averrage)&&(status==0))||(notic_lowpower))
            {
                sensor_data->state  = 9;
                if(notic_lowpower)
                {
                   time_send_lora=6000;
                   notic_lowpower=0; 
                }
                else
                {
                    goto_lowpower=1;
                }
            }
            else
            {
                sensor_data->state  = status;
            }
            
            crc = GetCRC(lora_tb, lora_tb[4]);
            lora_tb[lora_tb[4]+0] = crc>>8;
            lora_tb[lora_tb[4]+1] = crc;
            lora_transmit(lora_tb, lora_tb[4]+2);
            
            delay_ms(10);
            lora_m0(1);
            lora_m1(1);
            delay_ms(500);
//            printf("lora_send_message\r\n");
            
        }
    }
}
/***********************************************************************************
 * @brief  lora发送 
 * @retval 
***********************************************************************************/
void lora_send(uint8_t type)
{
    uint16_t crc;
    if((system_data.myaddress)&&(system_data.host_address[0]))
    {
        lora_m0(0);
        lora_m1(0);
        delay_ms(20);
        _Sensor_data_struct* sensor_data=(_Sensor_data_struct*)&lora_tb[7]; 
        lora_tb[0] = 0xb1;
        lora_tb[1] = system_data.host_address[0]>>8;
        lora_tb[2] = system_data.host_address[0];
        lora_tb[3] = type;
        lora_tb[4] = 12;
        lora_tb[5] = 1;
        lora_tb[6] = system_data.myaddress-1;
        sensor_data->gas    = system_data.gastype;
        switch(type)
        {
            case 4:
            {
                sensor_data->value  = system_data.range_ability;
                break;
            }
            case 5:
            {
                sensor_data->value  = system_data.low_alarm;
                break;
            }
            case 6:
            {
                sensor_data->value  = system_data.high_alarm;
                break;
            }
            default:
            {
                sensor_data->value  = mydata;
                break;
            }
        }
        
        switch(system_data.precision)
        {
            case 1:
            {
                sensor_data->propor = 0;
                break;
            }
            case 10:
            {
                sensor_data->propor = 1;
                break;
            }
            case 100:
            {
                sensor_data->propor = 2;
                break;
            }
            default:
            {
                break;
            }
        }
        sensor_data->unit = system_data.unit-1;
        sensor_data->state  = status;
        
        crc = GetCRC(lora_tb, lora_tb[4]);
        lora_tb[lora_tb[4]+0] = crc>>8;
        lora_tb[lora_tb[4]+1] = crc;
        lora_transmit(lora_tb, lora_tb[4]+2);
               
        delay_ms(10);
        lora_m0(1);
        lora_m1(1);
    } 
}



/***********************************************************************************
 * @brief  lora发送配置,低报高报量程 开机time_lora_config=0自动发一次
 * @retval 
***********************************************************************************/
void lora_send_config(void)
{
    static uint8_t state;
    if((system_data.myaddress)&&(system_data.host_address[0])&&(time_lora_config==0))
    {
        switch (state)
        {
        	case 0:lora_send(4);
                   state = 1;
                   lora_config_cnt= 50;break;
        		
        	case 1: if(lora_config_cnt){
                        return;
                    }
                    else{
                        lora_send(5);
                        state = 2;
                        lora_config_cnt= 50;
                    }break;
                    
        	case 2: if(lora_config_cnt){
                        return;
                    }
                    else{
                        lora_send(6);
                        state = 3;
                        lora_config_cnt= 50;
                    }break;

        	case 3: if(lora_config_cnt){
                        return;
                    }
                    else{
                        time_lora_config=TIME_LORA_CONFIG;
                        time_send_lora=0;      
                        state = 0;
                        printf("lora_send_config\r\n");
                    }break;                        
        	default:break;	
        }

    }
}
/***********************************************************************************
 * @brief  lora连续 发送间隔 RTC中断
***********************************************************************************/
void lora_config_timer(void)
{
    time_lora_config -= rtc_xsec;
    time_send_lora   -= rtc_xsec;
//    printf("time_lora=%d,%d\r\n",(int)time_lora_config,(int)time_send_lora);
    
    if(time_send_lora<=0)
    {
        time_send_lora = 0;
    }
    
    if(time_lora_config<=0)
    {
        time_lora_config = 0;
    }
}

/***********************************************************************************
 * @brief  lora计时
***********************************************************************************/
void lora_timer(void)
{
    if(lora_config_cnt){
        lora_config_cnt--;
    }
}
